// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Game from "./Game";
import HardModeGame from "./HardModeGame";
import Lobby from "./Lobby";
import Score from "./Score";

const {ccclass, property} = cc._decorator;

@ccclass
export default class ResultDialog extends cc.Component {

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

        let replayNode: cc.Node = cc.find('再玩一局', this.node);
        replayNode.on('touchstart', this.onReplay, this);
    }

    show() {

        this.node.active = true;

        let labelScore: cc.Label = cc.find('最终得分', this.node).getComponent(cc.Label);
        labelScore.string = '最终得分:' + Score.i.getScore();  // 显示分数

        let labelHistory: cc.Label = cc.find('历史最高分', this.node).getComponent(cc.Label);

        let Value = cc.sys.localStorage.getItem("scoreData");
        let data = JSON.parse(Value);

        labelHistory.string = '历史最高分:' + data.value;  // 显示分数
    }

    hide() {

        this.node.active = false;
    }

    // 重置历史最高分
    onResetHistoryScore() {

        HardModeGame.inst.scoreData.value = "0";

        let storeStr = JSON.stringify(HardModeGame.inst.scoreData);
        cc.sys.localStorage.setItem('scoreData' ,storeStr);

        this.onReplay();
    }

    onReplay() {

        this.hide();

        if(Lobby.inst.gameMode == 0) {
            Game.inst.startGame();
        }
        else if (Lobby.inst.gameMode == 1) {
            HardModeGame.inst.startHardModeGame();
        }
    }

    start () {

    }

    // update (dt) {}
}
